package org.atlantis.mongo.collection;

import org.atlantis.mongo.MongoFactory;

import com.mongodb.DB;

public class GetCollection {

	private static final String collName = "trial.datatypes";

	// 直接使用DB.getCollection来获取。获取10万次，耗时15ms。
	public void getCollByDefault() {
		DB db = MongoFactory.getDB();
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			db.getCollection(collName);
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Waste time: " + (endTime - startTime) + " ms.");
	}
	
	// 从缓存中获取。获取10万次，耗时195ms。
	public void getCollByCache() {
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < 100000; i++) {
			MongoFactory.getCollection(collName);
		}
		long endTime = System.currentTimeMillis();
		System.out.println("Waste time: " + (endTime - startTime) + " ms.");
	}

	public static void main(String[] args) {
		GetCollection trial = new GetCollection();
		trial.getCollByCache();
	}
}
